//=============================================================================
#include "CMap.h"

//=============================================================================
CMap::CMap() {
	Surf_Tileset = NULL;
}

//=============================================================================
bool CMap::OnLoad(char* File) {
    TileList.clear();

    FILE* FileHandle = fopen(File, "r");

    if(FileHandle == NULL) {
        return false;
    }

    for(int Y = 0;Y < MAP_HEIGHT;Y++) {
        for(int X = 0;X < MAP_WIDTH;X++) {
            CTile tempTile   ;
            fscanf(FileHandle, "%d:%d ", &tempTile.TileID, &tempTile.TypeID);
            //guardo las posiciones de los spawns;
            t_posicion nuevaPosicion;
            nuevaPosicion.X = X * TILE_SIZE;
            nuevaPosicion.Y = (Y-1) * TILE_SIZE;
            switch (tempTile.TileID){
                case TILE_ID_SPAWN_J:
                    SpawnJList.push_back(nuevaPosicion);
                    break;
                case TILE_ID_SPAWN_Z:
                    SpawnZList.push_back(nuevaPosicion);
                    break;
                case TILE_ID_PLANTA_I:
                    PlantasIList.push_back(nuevaPosicion);
                    tempTile.TileID=TILE_ID_TIERRA;
                    break;
                case TILE_ID_PLANTA_D:
                    PlantasDList.push_back(nuevaPosicion);
                    tempTile.TileID=TILE_ID_TIERRA;
                    break;
            }
            TileList.push_back(tempTile);
        }
        fscanf(FileHandle, "\n");
    }

    fclose(FileHandle);

    return true;
}

//-----------------------------------------------------------------------------
void CMap::OnRender(SDL_Surface* Surf_Display, int MapX, int MapY) {
	if(Surf_Tileset == NULL) return;

	int TilesetWidth  = Surf_Tileset->w / TILE_SIZE;
	int TilesetHeight = Surf_Tileset->h / TILE_SIZE;

	int ID = 0;

	for(int Y = 0;Y < MAP_HEIGHT;Y++) {
		for(int X = 0;X < MAP_WIDTH;X++) {
			/*if(TileList[ID].TypeID == TILE_TYPE_NONE) {
				ID++;
				continue;
			}*/

			int tX = MapX + (X * TILE_SIZE);
			int tY = MapY + (Y * TILE_SIZE);

			int TilesetX = (TileList[ID].TileID % TilesetWidth) * TILE_SIZE;
			int TilesetY = (TileList[ID].TileID / TilesetWidth) * TILE_SIZE;

			CSurface::OnDraw(Surf_Display, Surf_Tileset, tX, tY, TilesetX, TilesetY, TILE_SIZE, TILE_SIZE);

			ID++;
		}
	}
}

//=============================================================================
CTile* CMap::GetTile(int X, int Y) {
    int ID = 0;

    ID = X / TILE_SIZE;
    ID = ID + (MAP_WIDTH * (Y / TILE_SIZE));

    if(ID < 0 || ID >= TileList.size()) {
    	return NULL;
    }

    return &TileList[ID];
}

//=============================================================================
